package com.ro0tsh3ll.controller;

import com.alibaba.fastjson.JSONObject;
import com.ro0tsh3ll.tools.HttpClientUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2014/6/11.
 */
@Controller
@RequestMapping("/baiDu")
public class BaiduShortUrlController {

    @RequestMapping("createUrl")
    public String createUrl(String url,Model model) {
        return "baidu/createUrl";
    }

    @RequestMapping("sortUrl")
    public @ResponseBody Map<String, Object> sortUrl(String url) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String reUrl = HttpClientUtil.sendPostRequest("http://dwz.cn/create.php", "url="+url, true);
        JSONObject reJson = JSONObject.parseObject(reUrl);
        if (reJson.getInteger("status")!=0){
            modelMap.put("err_msg",reJson.getString("err_msg"));
        }else {
            modelMap.put("tinyurl",reJson.getString("tinyurl"));
        }
        return modelMap;
    }

    @RequestMapping("longUrl")
    public @ResponseBody Map<String, Object> longUrl(String url) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String reUrl = HttpClientUtil.sendPostRequest("http://dwz.cn/query.php", "tinyurl="+url, true);
        JSONObject reJson = JSONObject.parseObject(reUrl);
        if (reJson.getInteger("status")!=0){
            modelMap.put("err_msg",reJson.getString("err_msg"));
        }else {
            modelMap.put("longurl",reJson.getString("longurl"));
        }
        return modelMap;
    }
}